﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebSearch.Helpers
{
    public static class LinkHelper
    {
        const String FTP_PROTO = "ftp://";
        const String SMB_PROTO = "\\\\";

        public static String FileLabel(this HtmlHelper helper, String fullPath)
        {
            if (fullPath.StartsWith(FTP_PROTO))
            {
                return fullPath.Substring(fullPath.IndexOf('/', FTP_PROTO.Length) + 2);
            }
            else if (fullPath.StartsWith(SMB_PROTO))
            {
                return fullPath.Substring(fullPath.IndexOf('\\', SMB_PROTO.Length));
            }
            return fullPath;
        }

        public static String FileUri(this HtmlHelper helper, String fullPath)
        {
            if (fullPath.StartsWith(FTP_PROTO))
            {
                int pos = fullPath.IndexOf('/', FTP_PROTO.Length);
                String proto = fullPath.Substring(0, pos);
                String path = fullPath.Substring(pos + 2);
                return proto + HttpUtility.UrlPathEncode(path);
            }
            else if (fullPath.StartsWith(SMB_PROTO))
            {
                int pos = fullPath.IndexOf('\\', SMB_PROTO.Length);
                String host = fullPath.Substring(SMB_PROTO.Length, pos - SMB_PROTO.Length);
                String path = fullPath.Substring(pos + 1);
                return String.Format("file://{0}/{1}", host, HttpUtility.UrlPathEncode(path.Replace('\\', '/')));
            }
            return fullPath;
        }
    }
}